package controllers

import (
	//"encoding/json"
	//	"errors"
	//	"strconv"
	//	"strings"
	"fmt"
	"gouser/models"
	"library/common"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

// ToaDepartmentController operations for ToaDepartment
type ToaDepartmentController struct {
	beego.Controller
}

func init() {
	dbUser := beego.AppConfig.String("juanpuser_mysqluser")
	dbPass := beego.AppConfig.String("juanpuser_mysqlpass")
	dbHost := beego.AppConfig.String("juanpuser_mysqlhost")
	dbPort := beego.AppConfig.String("juanpuser_mysqlport")
	dbName := beego.AppConfig.String("juanpuser_mysqldb")
	maxIdleConn, _ := beego.AppConfig.Int("db_max_idle_conn")
	maxOpenConn, _ := beego.AppConfig.Int("db_max_open_conn")
	dbLink := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbUser, dbPass, dbHost, dbPort, dbName) + "&loc=Asia%2FShanghai"
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("juanpiUser", "mysql", dbLink, maxIdleConn, maxOpenConn)
	orm.Debug = true
}

// URLMapping ...
func (c *ToaDepartmentController) URLMapping() {

	c.Mapping("DepList", c.DepList)
}

// depList ...
// @Title search User
// @Description get organization struct
// @Param   appKey      query   string  false      "use appKey to check system"
// @Param   sign 		query   string  false      "use sign to check user"
// @Success 200 {object} map[string]interface{}{"code":200, "msg":Msg, "data":department}
// @Failure 403 map[string]interface{}{ "msg":Msg}
// @router /depList [get]
func (c *ToaDepartmentController) DepList() {
	parms := map[string]string{}
	if v := c.GetString("appKey"); v != "" {
		parms["appKey"] = v
	} else {
		c.Data["json"] = map[string]interface{}{"msg": "参数传递不完整;缺少appKey参数"}
		c.ServeJSON()
		return
	}
	if v := c.GetString("sign"); v != "" {
		parms["sign"] = v
	} else {
		c.Data["json"] = map[string]interface{}{"msg": "参数传递不完整;缺少sign参数"}
		c.ServeJSON()
		return
	}
	if common.CheckAPI(parms) {
		DepList := models.GetDepList()
		c.Data["json"] = map[string]interface{}{"code": 200, "msg": "获取成功", "data": DepList}
	} else {
		c.Data["json"] = map[string]interface{}{"msg": "非法访问"}
	}

	c.ServeJSON()
}
